//
//  UIBarButtonItem+Extension.swift
//  CWLiving
//
//  Created by 方正刚 on 2017/7/5.
//  Copyright © 2017年 方正刚. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    
// 类方法
//    class func createItem(imageName: String, highImageName: String, size: CGSize) -> UIBarButtonItem{
//        let btn = UIButton()
//        btn.setImage(UIImage(named: imageName), for: .normal);
//        btn.setImage(UIImage(named: highImageName), for: .highlighted)
//        btn.frame = CGRect(origin: CGPoint.zero, size: size)
//        
//        return UIBarButtonItem(customView: btn)
//    }
    
// swift更建议使用构造函数 
// 1> convenience 
// 2> 在构造函数中必须明确一个设计的构造函数(self)
    convenience init(imageName: String, highImageName: String = "", size: CGSize = CGSize.zero) {
        let btn = UIButton()
        btn.setImage(UIImage(named: imageName), for: .normal);
        if highImageName != "" {
            btn.setImage(UIImage(named: highImageName), for: .highlighted)
        }
        
        if size == CGSize.zero {
            btn.sizeToFit()
        }else {
            btn.frame = CGRect(origin: CGPoint.zero, size: size)
 
        }
        
        self.init(customView: btn)
    }
    
    
}
